package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;


    //Request URL: http://127.0.0.1:8500/api/user/wxLogin/wxLogin/0d3lRf200PIo1T1HI2000sz3xT2lRf2s


    @Operation(summary = "微信登录")
    @GlobalTransactional
    @GetMapping("wxLogin/{code}")
    public Result<Map<String,Object>> wxLogin(@PathVariable("code") String code){
        Map<String,Object> map = userInfoService.wxLogin(code);
        return Result.ok(map);
    }
    @GuiGuLogin
    @Operation(summary = "查询用户信息")
    @GetMapping("/getUserInfo")
    public Result<UserInfoVo> getUserInfo(){
        UserInfoVo userInfoVo = userInfoService.getUserInfoVo();
        return Result.ok(userInfoVo);
    }

    @GuiGuLogin
    @Operation(summary = "修改用户信息")
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody UserInfo userInfo){
        userInfo.setId(AuthContextHolder.getUserId());
        userInfoService.updateById(userInfo);
        return Result.ok();
    }

}
